setwd("c:/users/james/desktop/dropbox/transparency_and_democracy/transparencyindex/PAReplicationMaterials/Robustness Checks")

library(R2jags)
library(foreign)
library(data.table)
library(xtable)

baseline <- read.dta("c:/users/james/desktop/dropbox/transparency_and_democracy/transparencyindex/PAReplicationMaterials/Index Properties/HRV2013.dta")

for (i in 1:3){
  eval(parse(text=paste(sprintf("robust0%s0<-read.dta(\"Robust0%s0.dta\")",i,i))))
  eval(parse(text=paste(sprintf("robust00%s<-read.dta(\"Robust00%s.dta\")",i,i))))
  }

for (i in 1:3){
  for (j in 0:3){
  eval(parse(text=paste(sprintf("robust%s%s0<-read.dta(\"Robust%s%s0.dta\")",i,j,i,j))))
  }
}

TransValueCor<-matrix(data=NA, nrow=4, ncol=4)

TransValueCor[1,1] <- 1

for (i in 1:3){
eval(parse(text=sprintf("TransValueCor[1,(i+1)] <- cor(baseline$transparencyindex,robust0%s0$transparencyindex)",i)))
}

for (i in 1:3){
  for (j in 0:3){
  eval(parse(text=sprintf("TransValueCor[(i+1),(j+1)] <- cor(baseline$transparencyindex, robust%s%s0$transparencyindex)",i,j)))
  }
}


TransSDCor<-matrix(data=NA, nrow=4, ncol=4)

TransSDCor[1,1] <- 1

for (i in 1:3){
eval(parse(text=sprintf("TransSDCor[1,(i+1)] <- cor(baseline$transparencyindexsd,robust0%s0$transparencyindexsd)",i)))
}

for (i in 1:3){
  for (j in 0:3){
  eval(parse(text=sprintf("TransSDCor[(i+1),(j+1)] <- cor(baseline$transparencyindexsd, robust%s%s0$transparencyindexsd)",i,j)))
  }
}

TransDiffCor<-matrix(data=NA, nrow=4, ncol=4)

TransDiffCor[1,1] <- 1

for (i in 1:3){
eval(parse(text=paste(sprintf("TransDiffCor[1,(i+1)] <- cor(baseline$transparencyindexsd,robust0%s0$transparencyindexsd, use=\"complete.obs\")",i))))
}

for (i in 1:3){
  for (j in 0:3){
  eval(parse(text=paste(sprintf("TransDiffCor[(i+1),(j+1)] <- cor(baseline$transdiff, robust%s%s0$transdiff, use=\"complete.obs\")",i,j))))
  }
}

TransDifficultyDiscCor<-matrix(data=NA, nrow=3, ncol=4)

for (i in 1:3){
  TransDifficultyDiscCor[i,1]<-1
  eval(parse(text=sprintf("TransDifficultyDiscCor[1,(i+1)] <- cor(baseline$transparencyindex,robust00%s$transparencyindex)",i)))
  eval(parse(text=sprintf("TransDifficultyDiscCor[2,(i+1)] <- cor(baseline$transparencyindexsd, robust00%s$transparencyindexsd)",i)))
  eval(parse(text=paste(sprintf("TransDifficultyDiscCor[3,(i+1)] <-cor(baseline$transdiff, robust00%s$transdiff, use=\"complete.obs\")",i))))
  }

DifficultyDiscLabels<- c("Means", "Standard Dev.", "First Differences")

DifficultyDisc<-data.frame(DifficultyDiscLabels, TransDifficultyDiscCor)

TransValueCor.table<-xtable(TransValueCor, label="ValueCor")
TransSDCor.table <- xtable(TransSDCor, label="SDCor")
TransDiffCor.table <- xtable(TransDiffCor, label="DiffCor")


DifficultyDiscCor.table <- xtable(DifficultyDisc, label="DifficultyDisc", digits=3  )

sink("PriorRobustnessCorMatrices.txt")

TransValueCor.table
TransSDCor.table
TransDiffCor.table
DifficultyDiscCor.table  